package com.woniu.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.constant.Constant;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.shop.entity.dto.QueryServiceDTO;
import com.woniu.shop.entity.dto.StoreServiceDTO;
import com.woniu.shop.mapper.StoreServiceMapper;
import com.woniu.shop.pojo.StoreService;
import com.woniu.shop.service.IStoreServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * <p>
 * 门店服务表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Service
public class StoreServiceServiceImpl extends ServiceImpl<StoreServiceMapper, StoreService> implements IStoreServiceService {

    @Autowired(required = false)
    private StoreServiceMapper storeServiceMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result addService(StoreServiceDTO storeServiceDTO,String status) {
        Field[] fields = storeServiceDTO.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String methodName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
            Method method = null;
            try {
                method = storeServiceDTO.getClass().getMethod(methodName);
                Object value = method.invoke(storeServiceDTO);
                System.out.println(method.getName()+"=======");
                if (value == null ||value.toString().length() == 0) {
                    return Result.error(ResultCode.ERROR.getCode(), "请检查申请信息是否填写完整");
                }
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        StoreService storeService = new StoreService();
        BeanUtils.copyProperties(storeServiceDTO, storeService);
        //storeService.setServiceStatus(status);
        storeService.setIsDeleted("0");
        int flag = storeServiceMapper.insert(storeService);
        if (flag == 1){
            stringRedisTemplate.opsForSet().add(Constant.REDIS_DB_UPLOAD_IMAGE,storeService.getServicePicture());
            return Result.ok(ResultCode.SUCCESS.getCode(),"添加成功",flag);
        }
        return Result.error(ResultCode.ERROR.getCode(),"添加失败");
    }

    @Override
    public Result updateStatus(String id, String status) {

        Integer flag = storeServiceMapper.updateStatus(id, status);
        if (flag == 1) {
            return Result.ok();
        }
        return Result.error(flag);
    }

    @Override
    public List<StoreService> queryByType(String id) {
        return storeServiceMapper.queryByType(id);
    }

    @Override
    public PageResult<StoreService> queryServiceByShop(Integer currentPage, Integer currentSize, QueryServiceDTO queryServiceDTO) {
        LambdaQueryWrapper<StoreService> wrapper = new LambdaQueryWrapper<>();
        if (queryServiceDTO != null){
            wrapper.eq(!StringUtils.isEmpty(queryServiceDTO.getShopId()),StoreService::getShopId,queryServiceDTO.getShopId())
                    .eq(!StringUtils.isEmpty(queryServiceDTO.getServiceTypeId()),StoreService::getServiceTypeId,queryServiceDTO.getServiceTypeId())
                    .like(!StringUtils.isEmpty(queryServiceDTO.getServiceName()),StoreService::getServiceName,queryServiceDTO.getServiceName())
                    //.eq(!StringUtils.isEmpty(queryServiceDTO.getServiceStatus()),StoreService::getServiceStatus,queryServiceDTO.getServiceStatus())
                    .le(queryServiceDTO.getEndTime() != null,StoreService::getCreatedTime,queryServiceDTO.getEndTime())
                    .ge(queryServiceDTO.getStartTime() != null,StoreService::getCreatedTime,queryServiceDTO.getStartTime());
        }
        IPage<StoreService> page = new Page<>(currentPage,currentSize);
        IPage<StoreService> pages = storeServiceMapper.selectPage(page, wrapper);
        PageResult<StoreService> result = new PageResult<>(pages.getTotal(),pages.getRecords());
        return result;
    }

    @Override
    public StoreService getService(String id) {
        StoreService storeService = storeServiceMapper.selectById(id);
        return storeService;
    }


}
